# Makefile
CC = gcc
CLANG = clang
ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' | sed 's/aarch64/arm64/')
VMLINUX_H = vmlinux.h

# 用户态程序
USER_APP = prefetcher
BENCH_APP = benchmark

# eBPF 程序
EBPF_OBJ = prefetcher.bpf.o

# libbpf 生成的骨架文件
EBPF_SKEL = prefetcher.skel.h

# CFLAGS
CFLAGS = -g -Wall
LDFLAGS = -lelf -lz -lbpf

all: $(USER_APP) $(BENCH_APP)

$(EBPF_OBJ): prefetcher.bpf.c $(VMLINUX_H) prefetcher.h
	$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) -I. -c $< -o $@

$(EBPF_SKEL): $(EBPF_OBJ)
	bpftool gen skeleton $< > $@

$(USER_APP): prefetcher.c $(EBPF_SKEL)
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

$(BENCH_APP): benchmark.c
	$(CC) $(CFLAGS) $< -o $@

clean:
	rm -f $(USER_APP) $(BENCH_APP) $(EBPF_OBJ) $(EBPF_SKEL)

.PHONY: all clean